package com.dnxh.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dnxh.constant.CommonConstant;
import com.dnxh.constant.CustomResult;
import com.dnxh.entity.BasicInfo;
import com.dnxh.entity.Meeting;
import com.dnxh.entity.common.PageContext;
import com.dnxh.entity.request.CreateMeetingRequest;
import com.dnxh.entity.request.SearchAdminRequest;
import com.dnxh.mapper.MeetingMapper;
import com.dnxh.service.MeetingService;
import com.dnxh.util.JwtUtil;
import com.dnxh.util.component.MeetingUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhiqiu
 * @since 2022-04-12
 */
@Service
public class MeetingServiceImpl extends ServiceImpl<MeetingMapper, Meeting> implements MeetingService {
	
	@Resource
	private HttpServletRequest httpServletRequest;
	@Resource
	private MeetingMapper      meetingMapper;
	@Resource
	private MeetingUtil meetingUtil;
	
	@Override
	public CustomResult create (CreateMeetingRequest request) {
		Long meetingId = IdUtil.getSnowflakeNextId ();
		String token = httpServletRequest.getHeader (CommonConstant.TOKEN);
		if (CharSequenceUtil.isEmpty (token)) {
			log.error ("数据错误!无请求头!");
			return CustomResult.fail ("数据请求头未携带Token，请校验后重新发送!");
		}
		switch (request.getType ()){
			case MEETING:
				if (CollUtil.isEmpty (request.getGrade ()) && CollUtil.isEmpty (request.getList ())){
					return CustomResult.fail("meeting people is null");
				}
				meetingUtil.sendMeetingEmail (meetingId,request.getGrade (),request.getList ());
				break;
			case MATCH:
				break;
			default:
				return CustomResult.fail("meeting type is not allow");
		}
		String operationName = JwtUtil.decode (token).get (CommonConstant.JWT_USER_NAME).toString ();
		save (new Meeting ()
				.setId (meetingId)
				.setMName (request.getName ())
				.setLocation (request.getLocation ())
				.setTime (request.getTime ())
				.setType (request.getType ().name ())
				.setCreateTime (new Date ())
				.setCreateBy (operationName));
		return CustomResult.success ();
	}
	
	@Override
	public CustomResult search (SearchAdminRequest request) {
		if (request.getPageSize () == - 1) {
			request.setPageSize ((int) count ());
		}
		IPage<Meeting> iPage = new Page<Meeting> ()
				.setSize (request.getPageSize ())
				.setCurrent (request.getPageNum ());
		List<Meeting> result = meetingMapper.search (iPage, request.getUser ());
		return CustomResult.success (new PageContext<Meeting> ()
				.setList (result)
				.setCount (iPage.getTotal ()));
	}
	
	@Override
	public CustomResult delete (Long id) {
		meetingMapper.deleteMeeting (id);
		return CustomResult.success ();
	}
	
	@Override
	public CustomResult searchMeetingName (Long id) {
		Meeting meeting = getOne (Wrappers.<Meeting>lambdaQuery ()
				.select (Meeting::getMName)
				.eq (Meeting::getId,id));
		return CustomResult.success (meeting == null ? "电脑学会会员注册" : meeting.getMName ());
	}
}
